//index.js
const app = getApp()
const {
  envList
} = require('../../envList.js')

Page({
  data: {
    student: []
  },
  onLoad: function (options) {
    // Do some initialize when page load.
    wx.showLoading({
      title: '加载中',
    })
    wx.cloud.callFunction({
      // 云函数名称
      name: 'classCounter',
      // 传给云函数的参数
      data: {
        type: 'nameList',
        num: options['num'] ? options['num'] : 1,
      },
    })
      .then(res => {
        if (res.result.success) {
          console.log(res.result.data.student) // 3
          this.setData({
            student: res.result.data.student
          })
        } else {
          wx.showToast({
            title: res.result.msg,
            icon: 'error',
            duration: 2000
          })

        }
        wx.hideLoading()
      })
      .catch(console.error)
  },
  // Event handler.
  checkboxChange: function (e) {
    console.log(e.detail.value)
  },
  jumpPage(e) {
    wx.navigateTo({
      url: '/pages/counterhandle/index',
    })
  },
  formSubmit(e) {
    console.log('form发生了submit事件，携带数据为：', e.detail.value)
    wx.showLoading({
      title: '加载中',
    })
    wx.cloud.callFunction({
      name: 'classCounter',
      data: {
        type: 'upScore',
        ids: e.detail.value.ids
      }
    }).then(res => {
      if (res.result.success) {
        wx.showToast({
          title: res.result.data,
          icon: 'success',
          duration: 2000,
        }).then(res => {
          setTimeout(function () {
            wx.navigateBack({
              delta: 1,
            })
          }, 2000)
        })
      } else {
        wx.showToast({
          title: res.result.data,
          icon: 'error',
          duration: 2000
        })
      }
      setTimeout(function () {
        wx.hideLoading()
      }, 2000)
    }).catch(console.error)
  },

  formReset(e) {
    console.log('form发生了reset事件，携带数据为：', e.detail.value)
    //this.jumpPage();
  },
})